package com.cityos.android.mystudynestedscroll.ui.behavior

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.View.MeasureSpec
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.get
import androidx.viewpager2.widget.ViewPager2
import com.cityos.android.mystudynestedscroll.R
import com.cityos.android.mystudynestedscroll.common.utils.DisplayUtils
import com.cityos.android.mystudynestedscroll.common.utils.LoggerUtils
import com.gyf.immersionbar.ImmersionBar
import com.youth.banner.Banner

/**
 * @author hailong
 * @date 2024年03月04日 16:44
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 */
class UcViewPagerBehavior(context: Context? , attrs: AttributeSet?) : CoordinatorLayout.Behavior<ViewPager2>(context , attrs) {

    override fun layoutDependsOn(parent: CoordinatorLayout , child: ViewPager2 , dependency: View): Boolean {
        return dependency.id == R.id.uc_home_banner && dependency is Banner<* , *>
    }

    override fun onDependentViewChanged(parent: CoordinatorLayout , child: ViewPager2 , dependency: View): Boolean {
        if (layoutDependsOn(parent , child , dependency)) {
            child.translationY = dependency.translationY
            return true
        }
        return super.onDependentViewChanged(parent , child , dependency)
    }

    override fun onLayoutChild(parent: CoordinatorLayout , child: ViewPager2 , layoutDirection: Int): Boolean {
        val dependencies = parent.getDependencies(child)
        for (dependency in dependencies) {
            if (layoutDependsOn(parent , child , dependency)) {
                LoggerUtils.e("UcViewPagerBehavior.onLayoutChild = ${parent.measuredHeight}   ${dependency.bottom}")
                val measureHeight = parent.measuredHeight + DisplayUtils.dp2px(parent.context , 100f)
                child.layout(0 , dependency.bottom , parent.width , dependency.bottom + measureHeight)
                return true
            }
        }
        return super.onLayoutChild(parent , child , layoutDirection)
    }

    override fun onMeasureChild(parent: CoordinatorLayout , child: ViewPager2 , parentWidthMeasureSpec: Int , widthUsed: Int , parentHeightMeasureSpec: Int , heightUsed: Int): Boolean {
        val height: Int = if (ImmersionBar.hasNavigationBar(parent.context)) {
            DisplayUtils.getScreenHeight(parent.context) - DisplayUtils.dp2px(parent.context , 100f) - ImmersionBar.getStatusBarHeight(parent.context) - ImmersionBar.getNavigationBarHeight(parent.context)
        } else {
            DisplayUtils.getScreenHeight(parent.context) - DisplayUtils.dp2px(parent.context , 100f) - ImmersionBar.getStatusBarHeight(parent.context)
        }

        val width = DisplayUtils.getScreenWidth(parent.context)
        LoggerUtils.e("UcViewPagerBehavior.onMeasureChild = $height   ${parent.width}  ${DisplayUtils.getScreenHeight(parent.context)}")
        val withMeasure = MeasureSpec.makeMeasureSpec(width , MeasureSpec.EXACTLY)
        val heightMeasure = MeasureSpec.makeMeasureSpec(height , MeasureSpec.EXACTLY)
        child.measure(withMeasure , heightMeasure)
        return true
    }

}